package com.annoncesautomobile.vue;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;

import com.annoncesautomobile.R;
import com.annoncesautomobile.modele.util.ModelXML;

public class SplashScreenAffichagePub extends Activity {


	private static int SPLASH_TIME_OUT = 5000;
	private ModelXML modele;
	private ImageView imageView;
	private Object lock = new Object();
	
	private boolean[] locks;
	private boolean timer = false;
	private boolean intentNavigateur = false;
	
	private boolean isFromNotif = false;
	protected void setFlagOrStart(int what)
	{
		if(what > 999)
		{
			synchronized (lock) {
				timer = true;
			}
		}
		else
		{
			if(what == -1)
			{
				
			}
			else
			{
				synchronized (lock) {
					locks[what-1] = true;
				}
			}
			
		}
		synchronized (lock) {
			boolean isFull = true;
			for(int i = 0 ; (i < locks.length && isFull) ; ++i)
			{
				if( !locks[i] )
				{
					isFull = false;
				}
			}
			if(isFull && timer && !intentNavigateur)
			{
				Intent i = new Intent(SplashScreenAffichagePub.this, MainActivity.class);
				i.putExtra("fromNotif", isFromNotif);
				i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
				startActivity(i);
				overridePendingTransition(R.anim.grow_from_middle,R.anim.shrink_to_middle);
				finish();
			}
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		this.locks = new boolean[7];
		for(int i = 0 ; i < locks.length ; ++i)
		{
			locks[i] = false;
		}
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash_screen_affichage_pub);
		this.imageView = (ImageView) findViewById(R.id.imgPub);
		this.modele = ModelXML.getInstance();
		this.isFromNotif = getIntent().getBooleanExtra("fromNotif",false);
		System.out.println("ISFROMNOTIF PUB: " + isFromNotif);
		String fileName = getIntent().getStringExtra("picname");
		File filePath = getFileStreamPath(fileName);
		Drawable d = Drawable.createFromPath(filePath.toString());
		this.imageView.setImageDrawable(d);
		
		new getDatas().execute(1);
		new getDatas().execute(2);
		new getDatas().execute(3);
		new getDatas().execute(4);
		new getDatas().execute(5);
		new getDatas().execute(6);
		new getDatas().execute(7);
		
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				setFlagOrStart(1000);
			}
		}, SPLASH_TIME_OUT);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.splash_screen_affichage_pub, menu);
		return true;
	}

	public static int CHOIX_ENERGIE 			= 1;
	public static int CHOIX_DEPARTEMENT			= 2;
	public static int CHOIX_MARQUE_ANNONCE 		= 3;
	public static int CHOIX_MARQUE 				= 4;
	public static int CHOIX_CATEGORIE 			= 5;
	public static int CHOIX_10DERNIER 			= 6;

	private class getDatas extends AsyncTask<Integer, Void, Integer> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Integer doInBackground(Integer... arg0) {
			switch(arg0[0])
			{
			case 1 : modele.getDernieresAnnonces();break;
			case 2 : modele.getDepartements();break;
			case 3 : modele.getMarquesAnnonces(); break;
			case 4 : modele.getMarques();break;
			case 5 : modele.getCategories();break;
			case 6 : modele.getEnergies();break;
			}
			return arg0[0];
		}
		
		@Override
		protected void onPostExecute(Integer i)
		{
			setFlagOrStart(i);
		}
	}
	
	public void onClickPub(View v)
	{
		this.intentNavigateur = true;
		Intent i = new Intent(Intent.ACTION_VIEW);
		Uri u = Uri.parse(modele.getPubAutoPromo().getLien());
		i.setData(u);
		startActivityForResult(i,0); 
		
	}
	
	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data) 
	{
		System.out.println("On activity result");
		this.intentNavigateur = false;
		setFlagOrStart(-1);
	}
}
